{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#   [Py4Hw User Guide](../UserGuide.ipynb)> 4.1 The Simulator\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The simulator can be obtained from the HWSystem.\n",
    "\n",
    "The current simulator is implemented as a cycle based simulator.\n",
    "You basically call the method clk to advance the number of clocks you want."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import py4hw\n",
    "\n",
    "sys = py4hw.HWSystem()\n",
    "sim = sys.getSimulator()\n",
    "sim.clk(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The simulator object has the following fields and methods\n",
    "\n",
    "|Field| Description|\n",
    "| :-- | :-- |\n",
    "| total_clks | The number of clocks run from the initial time of the session |\n",
    "| sys | The HWSystem associated with the simulator |\n",
    "| listeners | A list of the objects that listen to simulator events |\n",
    "\n",
    "|Method| Description|\n",
    "| :-- | :-- |\n",
    "| __init__ | Constructor |\n",
    "|  propagateAll | Propagates all combinational circuits |\n",
    "| topologicalSort | We segment the circuit by clock drivers. Sorts all the elements of the circuit so that cycle-base simulation is possible. Clockables do not require any order. Propagatables must be sorted in propagation order, so when we insert we should check if there are elements in the list that depend on the current item, and in this case we should place it before them |\n",
    "| getOrCreateClockDriverSimulator | get a clock driver simulator |\n",
    "| findFirstDependentPosition | We look at the outputs of the provided circuit and find all the dependent cells, then we locate them in |\n",
    "| clk | Advance a number of clock cycles |\n",
    "| _clk_cycle | Advance one clock cycle |\n",
    "| stop | Stop the simulation |\n",
    "| addListener | Add a listener of the simulator events |\n",
    "| _notifyListeners | Notify the listeners of a simulator event |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
